package com.heima.wemedia.controller.v1;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.WmChannelDto;
import com.heima.model.common.pojos.wemedia.WmChannel;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 陈辉
 * @data 2024 9:39
 */
@RestController
@RequestMapping("/api/v1/channel")
@Slf4j
public class WmChannelController {

    @Autowired
    private WmChannelService wmChannelService;

    /**
     * 加载所有频道
     *
     * @return
     */
    @GetMapping("/channels")
    @Cacheable(cacheNames = "channel",key = "channelList")
    public ResponseResult channels(){
        List<WmChannel> list = wmChannelService.list();
        return ResponseResult.okResult(list);
    }

    @PostMapping("/save")
    public ResponseResult saveChannel(@RequestBody WmChannelDto channelDto){
        //1.先清缓存，再改数据库库：重大错误
        //一定要先改数据库然后再清缓存：可能存在短时间的数据不一致，最终数据一致即可
        log.info("传入频道信息为：{}",channelDto);
        return wmChannelService.saveChannel(channelDto);
    }
    @PostMapping("/list")
    public ResponseResult pageQuery(@RequestBody WmChannelDto channelDto) {
        log.info("传入分页搜索信息为：{}", channelDto);
        return wmChannelService.pageQuery(channelDto);
    }
    @PostMapping("/update")
    public ResponseResult update(@RequestBody WmChannelDto channelDto) {
        log.info("传入预修改的频道信息为：{}",channelDto);
        return wmChannelService.upate(channelDto);
    }


    @GetMapping("/del/{id}")
    public ResponseResult deleteChannel(@PathVariable Integer id) {
        log.info("传入预删除的频道id为： {}",id);
        return wmChannelService.deleteChannel(id);
    }
}
